<?php


namespace app\admin\controller\system;

use app\base\model\system\SystemMenus;
use app\core\exception\ErrorCode;
use app\core\util\ToolUtil;;
use app\base\model\system\SystemRole as RoleModel;
use app\admin\controller\adminAuthController;

class SystemRoleController extends adminAuthController
{
    protected $needRecordAdminOperationLogAction = ['saveRole', 'deleteRole'];
    /**
     * 获取管理员列表
     *
     * @return \think\Response
     */
    public function geRoleList()
    {
        $where = ToolUtil::getMore([['status', ''], ['role_name', ''],], $this->request);
        $where['level'] = $this->adminInfo['level'];
        $list = RoleModel::getRoleList($where);
        return $this->successReturn($list);
    }

    /**
     * 获取简单地角色信息.
     *
     * @return \think\Response
     */
    public function geRoleListByField()
    {
        $role_list = RoleModel::field('role_id as value,role_name as label')->where('level', bcadd($this->adminInfo['level'], 1, 0))->where('status', 1)->select();
        return $this->successReturn($role_list);
    }

    /**
     * 获取权限列表
     *
     * @return \think\Response
     */
    public function geAuthorityList()
    {
        $authority_list = $this->adminInfo['level'] == 0 ? SystemMenus::ruleList() : SystemMenus::rolesByRuleList($this->adminInfo['role_ids']);
        return $this->successReturn($authority_list);
    }

    /**
     * Desc: 保存新添加的角色
     * @return \think\Response
     */
    public function saveRole()
    {
        $role_id = input('role_id');
        $data = ToolUtil::postMore(['role_name', ['status', 0], ['checked_menus', [], '', 'menu_ids']], $this->request);
        if (!$data['role_name']) {
            return $this->errorReturn(ErrorCode::COMMON_LACK_PARAMS, '请输入身份名称');
        }

        if (empty($role_id) && RoleModel::where('role_name', $data['role_name'])->find()) {
            return $this->errorReturn(ErrorCode::COMMON_LACK_PARAMS, '角色[' . $data['role_name'] . ']已存在');
        }

        if (!is_array($data['menu_ids']) || !count($data['menu_ids'])) {
            return $this->errorReturn(ErrorCode::COMMON_LACK_PARAMS, '请选择最少一个权限');
        }

        foreach ($data['menu_ids'] as $v) {
            $pid = SystemMenus::where('menu_id', $v)->value('pid');
            if (!in_array($pid, $data['menu_ids'])) {
                $data['menu_ids'][] = $pid;
            }
        }
        $data['menu_ids'] = implode(',', $data['menu_ids']);

        if ($role_id) {
            $data['last_update_admin_id'] = $this->adminId;
            $res = RoleModel::edit($data, $role_id);
        } else {
            $data['create_admin_id'] = $this->adminId;
            $data['level'] = $this->adminInfo['level'] + 1;
            $res = RoleModel::set($data);
        }
        return $res ? $this->successReturn() : $this->errorReturn();
    }

    /**
     * 获取已存在的角色的详细信息.
     *
     * @return \think\Response
     */
    public function getRoleInfoByRoleId()
    {
        $role_id = input('role_id');
        if (ToolUtil::notIsValidId($role_id)) {
            return $this->errorReturn(ErrorCode::COMMON_LACK_PARAMS, 'role_id不合法');
        }
        $role_info = RoleModel::get($role_id);
        if (empty($role_info)) {
            return $this->errorReturn(ErrorCode::COMMON_LACK_PARAMS, '角色不存在');
        }
        $authority_list = $this->adminInfo['level'] == 0 ? SystemMenus::ruleList() : SystemMenus::rolesByRuleList($this->adminInfo['role_ids']);
        $data['role_info'] = $role_info;
        $data['authority_list'] = $authority_list;
        return $this->successReturn($data);
    }


    /**
     * 删除指定的角色
     *
     * @return \think\Response
     */
    public function deleteRole()
    {
        $role_id = input('role_id');
        if (ToolUtil::notIsValidId($role_id)) {
            return $this->errorReturn('role_id不合法');
        }
        $role_info = RoleModel::get($role_id);
        if (empty($role_info)) {
            return $this->errorReturn(ErrorCode::COMMON_LACK_PARAMS, '角色不存在');
        }

        $res = RoleModel::del($role_id);

        return $res ? $this->successReturn() : $this->errorReturn(ErrorCode::COMMON_ERROR);
    }
}
